<template>
  <div>
    <el-dialog id="achieve_dialog" :title="title" :visible.sync="dialogVisible" width="30%" @open="open" class="mt-one">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划状态:" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0" :disabled="pending ||underway">待配送</el-radio>
            <el-radio :label="1" :disabled="pending ||underway||suspend">配送中</el-radio>
            <el-radio :label="2">已配送</el-radio>
            <el-radio :label="3" :disabled="underway||suspend">产线暂停</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {editstatus, editsuspend} from "@/api/prodManger/inProdPlan"
import {listData} from "@/api/system/dict/data";

export default {
  name: "EditControl",
  props: {
    title: {
      type: String,
      default: '',
    },
    linedatas: {
      type: Array,
    }
  },
  data() {
    return {
      pending: false,//待配送
      underway: false,//配送中
      suspend: false,//暂停
      idslistdata: null, //需要修改的id
      dialogVisible: false,//是否展示弹框
      form: {//表格数据
        status: null
      },
      rules: {//校验规则
        status: [{required: true, message: "请选择需要更改的计划状态", trigger: "change"}],
      },
      stocklist: null,//计划状态
    }
  },
  methods: {
    open() {
      this.pending = false;
      this.underway = false;
      this.suspend = false;
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      this.getStatusList();
      this.form.status = null;
      if (this.$props.linedatas[0].status == 0) {
        this.pending = true;
      } else if (this.$props.linedatas[0].status == 1) {
        this.underway = true;
      } else if (this.$props.linedatas[0].status == 3) {
        this.suspend = true;
      }
    },
    // 数据字典
    getStatusList() {
      let data = {
        dictType: "task_status",
      };
      return listData(data).then((res) => {
        this.stocklist = res.data.list;
      })
    },
    // 确定按钮
    confirm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.status == 3) {
            let obj = {};
            obj.plName = this.$props.linedatas[0].prodLineName;
            obj.isSuspend = true;
            obj.status = 3;
            obj.ids = this.$props.linedatas.map(item => {
              return item.id
            })
            editsuspend(obj).then(res => {
              this.$message.success('修改成功');
              this.dialogVisible = false;
              this.$emit('refresh');
            })
          } else if (this.$props.linedatas[0].status == 3) {
            let obj = {};
            obj.plName = this.$props.linedatas[0].prodLineName;
            obj.isSuspend = false;
            if (this.form.status == 0) {
              obj.status = 0;
              obj.ids = this.$props.linedatas.map(item => {
                return item.id
              })
              editsuspend(obj).then(res => {
                this.$message.success('修改成功');
                this.dialogVisible = false;
                this.$emit('refresh');
              })
            } else if (this.form.status == 2) {
              let parameter = this.$props.linedatas.map(item => {
                return {
                  "id": item.id,
                  "sort": item.sort,
                  "orderNumber": item.orderNumber,
                  "productCode": item.productCode,
                  "prodLineName": item.prodLineName,
                  "orderProductCount": item.orderProductCount,
                  "beginStationId": item.beginStationId,
                  "endStationId": item.endStationId,
                  "emptyBoxHoming": item.emptyBoxHoming,
                  "status": this.form.status,
                }
              })
              editstatus(parameter).then(res => {
                this.$message.success('修改成功');
                this.dialogVisible = false;
                this.$emit('refresh');
              })
            }
          } else {
            let parameter = this.$props.linedatas.map(item => {
              return {
                "id": item.id,
                "sort": item.sort,
                "orderNumber": item.orderNumber,
                "productCode": item.productCode,
                "prodLineName": item.prodLineName,
                "orderProductCount": item.orderProductCount,
                "beginStationId": item.beginStationId,
                "endStationId": item.endStationId,
                "emptyBoxHoming": item.emptyBoxHoming,
                "status": this.form.status,
              }
            })
            editstatus(parameter).then(res => {
              this.$message.success('修改成功');
              this.dialogVisible = false;
              this.$emit('refresh');
            })
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.dialogVisible = false;
    }
  },
}
</script>

<style scoped>
</style>
